<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1>🏪 饿了没</h1>
        <p>商家管理系统</p>
      </div>
      
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label>商家ID</label>
          <input 
            v-model="form.businessId" 
            type="text" 
            placeholder="请输入商家ID"
            required
          />
        </div>
        
        <div class="form-group">
          <label>密码</label>
          <input 
            v-model="form.password" 
            type="password" 
            placeholder="请输入密码"
            required
          />
        </div>
        
        <button type="submit" class="login-btn" :disabled="loading">
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>
      
      <div v-if="error" class="error-message">
        {{ error }}
      </div>
      
      <div class="demo-info">
        <p>演示账号：</p>
        <p>ID: 10001 | 密码: 123456</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        businessId: '',
        password: ''
      },
      loading: false,
      error: ''
    }
  },
  methods: {
    async handleLogin() {
      this.loading = true
      this.error = ''
      
      try {
        const response = await this.$axios.post('/elm/BusinessManagementController/login', null, {
          params: {
            businessId: this.form.businessId,
            password: this.form.password
          }
        })

        console.log('登录响应:', response.data) // 调试日志
        
        if (response.data && (response.data.code === 200 || response.data.code === '200')) {
          const businessData = response.data.result
          
          // 验证返回的数据结构
          if (!businessData) {
            this.error = '服务器返回数据为空'
            return
          }
          
          if (!businessData.businessId) {
            this.error = '商家ID不存在，请检查登录信息'
            return
          }
          
          this.$store.dispatch('login', {
            businessId: businessData.businessId,
            businessName: businessData.businessName || '未知商家',
            token: businessData.token || 'default-token-' + Date.now()
          })
          this.$router.push('/dashboard')
        } else {
          this.error = response.data?.message || '登录失败'
        }
      } catch (error) {
        console.error('登录失败:', error)
        this.error = '登录失败，请检查网络连接'
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-card {
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  font-size: 32px;
  color: #333;
  margin-bottom: 10px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.form-group input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
}

.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.error-message {
  color: #e74c3c;
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
}

.demo-info {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
  text-align: center;
  font-size: 12px;
  color: #999;
}
</style>
